package com.shiliao.app.splash;

import android.content.Context;
import android.text.TextUtils;

import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.YMServerUtil;
import com.yimei.framework.mvp.presenter.IBasePresenter;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import java.util.HashMap;
import java.util.Map;

import retrofit2.http.PATCH;

/**
 * Created by yinzhiqiang on 2018/1/8.
 */

public class WelcomePresenter extends IBasePresenter<IWelcomeService> {
    private WelcomeView welcomeView;
    private IWelcomeService welcomeService;

    public WelcomePresenter(WelcomeView welcomeView) {
        this.welcomeView = welcomeView;
        welcomeService = new WelcomeImpl();
        setService(welcomeService);
    }

    public void request(String userId) {
        Map param = new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            param.put("user_id", userId);
        }
        LogUtil.e("yzq", "========" + userId);
        welcomeService.request(YMServerUtil.ACTION_GET_GUIDANCE, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", "............." + result.toString());
                String share = ParseHelper.getString(result.toString(), "sharing");
                String infosetting = ParseHelper.getString(result.toString(), "infosetting");
                String dailyremind = ParseHelper.getString(result.toString(), "dailyremind");
                welcomeView.getGuidance(share, infosetting, dailyremind);
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });

    }


    public void requestIsExamine(String userId){
        Map param = new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            param.put("user_id", userId);
        }
        welcomeService.request(YMServerUtil.ACTION_IS_AUDITOR, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yyz",result.toString());
              String auditor=ParseHelper.getString(result.toString(),"is-auditor");
               welcomeView.isAuditor(auditor);
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void resquestUserId(Map param, final Context context) {

        welcomeService.requestUserId(YMServerUtil.GET_USER_ID, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                String userId = ParseHelper.getString(result.toString(), "user_id");
                LogUtil.e("yzq", userId);
                YMUserManager.saveUserId(context, userId);//保存用户id
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

//    public void requestUserId(String device,String phone){
//        Map param = new HashMap();
//        boolean bNeedGetuserid=true;
//        String local_userId="0";
//        LogUtil.e("yzq", device+"手机号"+ phone);
//        //added by luyi, 当 deviceid, phone 都获取不到时，通过随机数作为 device id，申请 user_id，一旦申请成功，之后就不再申请
//        if((device.length()<6)&&(phone.length()!=11))
//        {
//            //device id, phone no 都无效
//            local_userId=welcomeView.getUserId();
//            if(local_userId.equals("0")) {
//                double r=Math.random();
//                r=r*100000000;
//                int i=(int)r;
//                device=String.valueOf(i);
//            } else {
//                bNeedGetuserid=false;
//            }
//        }
//
//        if(phone.length()==11) {
//            //如果 phone no 有效，优先根据 phone no 获得 user_id
//            param.put("device_id","");
//            param.put("cellphone_no", phone);
//        } else {
//            param.put("device_id",device);
//            param.put("cellphone_no", phone);
//        }
//
//        LogUtil.e("yzq", device+"手机号"+ phone);
//        LogUtil.e("yzq",bNeedGetuserid+"======");
//        if(bNeedGetuserid) {
//            welcomeService.requestUserId(YMServerUtil.GET_USER_ID, param, new ICallBack() {
//                @Override
//                public void onSuccess(int statusCode, Object result) {
//
//                    LogUtil.e("yzq", result.toString());
//                    String userId = ParseHelper.getString(result.toString(), "user_id");
//                    LogUtil.e("yzq", userId);
//                    welcomeView.saveUserId(userId);//保存用户id
//                    welcomeView.toMainActivity();
//                    //check 该用户是否有新消息
//                    checkMessage(userId);
//                }
//
//                @Override
//                public void onFail(int statusCode, Object result) {
//                    LogUtil.e("yzq", result.toString());
//                }
//            });
//        } else {
//            checkMessage(local_userId);
//        }
//    }

//    private void checkMessage(String userId) {
//        Map param = new HashMap();
//        //  JSONObject param=new JSONObject();
//
//        param.put("user_id", userId);
//
//        welcomeService.request(YMServerUtil.CHECK_MESSAGE, param, new ICallBack() {
//            @Override
//            public void onSuccess(int statusCode, Object result) {
//                LogUtil.e("yzq",result.toString());
//                String retcode= ParseHelper.getString(result.toString(),"new_msg_no");
//                if (!retcode.equals("0"))
//                {
//                    YMUserManager.bHasnewmessage=true;
//                    MessageEvent mEvent = new MessageEvent();
//                    mEvent.eventId = MessageEvent.EVENT_ID_NEW_MSG_STATUS_CHANGE;
//                    EventBus.getDefault().post(mEvent);
//                }
//            }
//
//            @Override
//            public void onFail(int statusCode, Object result) {
//                LogUtil.e("yzq",result.toString());
//            }
//        });
//    }
}
